package com.southwind.springboottest.controller;

import com.southwind.springboottest.entity.Healthy;
import com.southwind.springboottest.repository.HealthyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/healthy")
public class HealthyHandler {

    @Autowired
    private HealthyRepository healthyRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Healthy> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return healthyRepository.findAll(pageable);
    }

    @PostMapping("/save")
    public String save(@RequestBody Healthy bodyCheck){
        Healthy result = healthyRepository.save(bodyCheck);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @GetMapping("/findById/{id}")
    public Healthy findById(@PathVariable("id") Integer id){
        return healthyRepository.findById(id).get();
    }

    @PutMapping("/update")
    public String update(@RequestBody Healthy bodyCheck){
        Healthy result = healthyRepository.save(bodyCheck);
        if(result!=null){
            return "success";
        }else{
            return "error";
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        healthyRepository.deleteById(id);
    }
}
